package com.maxatime.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.maxatime.common.enums.BusinessEnum;
import com.maxatime.pojo.dto.GradeDTO;
import com.maxatime.pojo.entity.Classroom;
import com.maxatime.pojo.entity.DetailGrade;
import com.maxatime.pojo.entity.EmployeeInfo;
import com.maxatime.pojo.entity.Grade;
import com.maxatime.pojo.entity.GradeStudentRelation;
import com.maxatime.pojo.entity.StudentBasicInfo;
import com.maxatime.pojo.vo.GradeVO;
import com.maxatime.service.GradeService;
import com.maxatime.service.GradeStudentRelationService;
import com.maxatime.service.StudentBasicInfoService;
import com.maxatime.util.MyContent;



@Controller
@RequestMapping("/grade")
public class gradeController {
	// 日志
	Logger log = LogManager.getLogger();

	@Autowired
	GradeService gradeService;
	
	@Autowired
	StudentBasicInfoService studentBasicInfoService;
	
	@Autowired
	GradeStudentRelationService gradeStudentRelationService;///点详情查询学生表
	
	/**
	 * 
	 * @Title: cha   
	 * @Description: TODO(班级管理系统)   
	 * @param: @return      
	 * @return: String      
	 * @throws   
	 * @author: ssb    
	 * @date:   2023年10月30日 下午2:07:43   
	 * @version V1.0
	 */
	@RequestMapping("/cha")
	public String cha() {
		return "Grade/grade";
	}
	
	
	  @RequestMapping("/queryData")
	  @ResponseBody 
	  public Map<String, Object> queryData(@RequestBody String jsonStr){ 
		  JSONObject jsonObj = JSONObject.parseObject(jsonStr);
		  String gradeName = jsonObj.getString("gradeName");
		  String classTeacherName = jsonObj.getString("classTeacherName");
		  String lecturerName = jsonObj.getString("lecturerName");
		//分页
			int pageNum = jsonObj.getIntValue("pageNum");
			int pageSize = jsonObj.getIntValue("pageSize");
			
		  Map<String, Object> m = new HashMap<String, Object>();
		  GradeDTO gradeDTO = new GradeDTO();
		  gradeDTO.setGradeName(gradeName);
		  gradeDTO.setClassTeacherName(classTeacherName);
		  gradeDTO.setLecturerName(lecturerName);
		  gradeDTO.setPageNum(pageNum);
		  gradeDTO.setPageSize(pageSize);
		  
		  Map<String, Object> map = gradeService.listData(gradeDTO);
		  
		  PageInfo<GradeDTO> pageInfo = new PageInfo<>((List<com.maxatime.pojo.dto.GradeDTO>) map.get("list"));  
	        System.out.println("总记录数："+pageInfo.getTotal());  
	        System.out.println("总页数："+pageInfo.getPages());  
	        System.out.println("一页的大小："+pageInfo.getSize()); 
		  
		  	m.put("data", map.get("list"));
			m.put("pageInfo", map.get("pageInfo"));
		    m.put("code", "0000");
		    m.put("msg", "交易成功");
		  return m; 
	  }
	  //新增
	  @RequestMapping("/saveData")
	  @ResponseBody
	  public Map<String , Object> saveData(@RequestBody GradeDTO gradeDTO){
		  Map<String, Object> m = new HashMap<String, Object>();
			
			gradeService.insertSelective(gradeDTO);
			
			m.put(MyContent.CODE, BusinessEnum.SUCCESS.getCode());
			m.put(MyContent.MSG, BusinessEnum.SUCCESS.getDesc());
		  return m;
	  }
	  //查询班主任，讲师和教室
	  @RequestMapping("/teacher")
	  @ResponseBody
	  public Map<String, Object> teacher(@RequestBody GradeVO gradeVO){
		  Map<String, Object> m =new HashMap<String, Object>();
		  EmployeeInfo employeeInfo=new EmployeeInfo();
		  
		  Classroom classroom =new Classroom();
		  List<EmployeeInfo> list = gradeService.teacher(employeeInfo);
		  List<Classroom> list1 =gradeService.classroom(classroom);
		  m.put(MyContent.CODE, BusinessEnum.SUCCESS.getCode());
		  m.put(MyContent.MSG, BusinessEnum.SUCCESS.getDesc());
		  m.put(MyContent.DATA, list);
		  m.put("classroom", list1);
		  return m;
	  }
	  
	  //反显gradeId
	  @RequestMapping("/findgradeId")
	  @ResponseBody
	  public Map<String, Object> findgradeId(@RequestBody String jsonStr){
		  JSONObject jsonObj = JSONObject.parseObject(jsonStr);
		  
		  String gradeId = jsonObj.getString("gradeId");
		  log.info("gradeId::::"+gradeId);
		  GradeDTO gradeDTO = gradeService.findgradeId(gradeId);
		  
		  Map<String, Object> m = new HashMap<String, Object>();
		  m.put(MyContent.CODE, BusinessEnum.SUCCESS.getCode());
		  m.put(MyContent.MSG, BusinessEnum.SUCCESS.getDesc());
		  m.put(MyContent.DATA, gradeDTO);
		  return m;
	  }
	  
	  //删除
	  @RequestMapping("/deleteData")
	  @ResponseBody
	  public Map<String , Object> deleteData(@RequestBody String jsonStr){
		  JSONObject jsonObj = JSONObject.parseObject(jsonStr);
		  String gradeId = jsonObj.getString("gradeId");
		  log.info("gradeId===="+gradeId);
		  gradeService.deleteByPrimaryKey(gradeId);
		  Map<String, Object> m = new HashMap<String, Object>();
		  m.put(MyContent.CODE, BusinessEnum.SUCCESS.getCode());
		  m.put(MyContent.MSG, BusinessEnum.SUCCESS.getDesc());
		  return m;
	  }
	  
	  //修改
	  @RequestMapping("/updateData")
	  @ResponseBody
	  public Map<String , Object> updateData(@RequestBody String jsonStr){
		  JSONObject jsonObj = JSONObject.parseObject(jsonStr);
		  String gradeId = jsonObj.getString("gradeId");
		  String gradeName = jsonObj.getString("gradeName");
		  String gradeDescribe = jsonObj.getString("gradeDescribe");
		  String classTeacherName = jsonObj.getString("classTeacherName");
		  String lecturerName = jsonObj.getString("lecturerName");
		  String classroomClassroomNumber = jsonObj.getString("classroomClassroomNumber");//所在教室
		  
		  GradeDTO gradeDTO = new GradeDTO();
		  gradeDTO.setGradeId(gradeId);
		  gradeDTO.setGradeName(gradeName);
		  gradeDTO.setGradeDescribe(gradeDescribe);
		  gradeDTO.setClassTeacherName(classTeacherName);
		  gradeDTO.setLecturerName(lecturerName);
		  gradeDTO.setClassroomClassroomNumber(classroomClassroomNumber);
		  gradeService.updateByPrimaryKeySelective(gradeDTO);
		  Map<String , Object> m = new HashMap<>();
		  m.put(MyContent.CODE, BusinessEnum.SUCCESS.getCode());
		  m.put(MyContent.MSG, BusinessEnum.SUCCESS.getDesc());
		  m.put(MyContent.DATA, gradeDTO);
		return m;
		  
	  }
	  //跳转详情页面
	  @RequestMapping("/xq")
	  public String xq(@RequestParam String gradeId, Model model) {
		  Grade grade = new Grade();
		  grade =  gradeService.selectByPrimaryKey(gradeId);
		  model.addAttribute("grade", grade);
		  return "Grade/xiangqing";
	  }
	
	  //详情查询学生和班级表
	 @RequestMapping("/selectgradeId")
	 @ResponseBody
	 public Map<String, Object> selectgradeId(@RequestBody String jsonStr){
		 JSONObject jsonObject = JSONObject.parseObject(jsonStr);
		 String gradeId = jsonObject.getString("gradeId");
		 Map<String, Object> m = new HashMap<>();
		 List<DetailGrade> list = gradeStudentRelationService.listData(gradeId);
		  m.put(MyContent.CODE, BusinessEnum.SUCCESS.getCode());
		  m.put(MyContent.MSG, BusinessEnum.SUCCESS.getDesc());
		  m.put(MyContent.DATA, list); 
		 return m;
	 }
	 //详情删除
	 @RequestMapping("/xqdelete")
	 @ResponseBody
	 public Map<String,Object> xqdelete(@RequestBody String jsonStr){
		 JSONObject jsonObject = JSONObject.parseObject(jsonStr);
		 String studentId = jsonObject.getString("studentId");
		 String gradeId = jsonObject.getString("gradeId");
		 Map<String, Object > m = new HashMap<>();
		 int a = gradeStudentRelationService.xqdelete(studentId);
		 gradeService.updateStu(gradeId);
		 m.put(MyContent.CODE, BusinessEnum.SUCCESS.getCode());
		  m.put(MyContent.MSG, BusinessEnum.SUCCESS.getDesc());
		 return m;
	 }
	   //详情新增查询详情
	  @RequestMapping("/queryxq")
	  @ResponseBody
	  public Map<String, Object> queryxq(@RequestBody DetailGrade detailGrade){
		  Map<String , Object> m = new HashMap<>();
		  List<DetailGrade> list=gradeStudentRelationService.selectByPrimaryKey(detailGrade);
		  m.put(MyContent.CODE, BusinessEnum.SUCCESS.getCode());
		  m.put(MyContent.MSG, BusinessEnum.SUCCESS.getDesc());
		  m.put(MyContent.DATA, list);
		  return m;
	  }
	//详情新增
	  @RequestMapping("/xqinsert")
	  @ResponseBody
	  public Map<String, Object> xqinsert(@RequestBody GradeStudentRelation record){
		  Map<String , Object> m = new HashMap<>();
		  int xz=gradeStudentRelationService.insertSelective(record);
		  gradeService.updateStu(record.getGradeGradeId());
		  m.put(MyContent.CODE, BusinessEnum.SUCCESS.getCode());
		  m.put(MyContent.MSG, BusinessEnum.SUCCESS.getDesc());
		  m.put(MyContent.DATA, xz);
		  return m;
	  }
	  
//	  //柱状形态图全查
//	  @RequestMapping("/zzt")
//	  @ResponseBody
//	  public Map<String, Object> zzt(@RequestBody GradeDTO gradeDTO){
//		  Map<String, Object> m = new HashMap<>();
//		  List<GradeDTO> abc = gradeService.listData(gradeDTO);
//		  m.put(MyContent.CODE, "6666");
//		  m.put(MyContent.MSG, "查询成功");
//		  m.put(MyContent.DATA, abc);
//		  return m;
//	  }
}
